package com.thumbnails;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.thumbnails.MyImageLoader.ImageCallBack;

public class ListAdapter extends BaseAdapter {

	private File[] lists;
	private LayoutInflater inflater;
	private ArrayList<Integer> urls= new ArrayList<Integer>();
	private List<String> paths= new ArrayList<String>();
	private MyImageLoader imageLoader = new MyImageLoader();
	private ContentHolder holder= null;
	 
	public ListAdapter(File[] lists, Context context, ListView listView) {
		
		this.lists= lists;
		inflater= LayoutInflater.from(context);
		listView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_FLING:
					
					System.out.println("SCROLL_STATE_FLING");//
					imageLoader.loadDrawable(true);
					break;
					
				case OnScrollListener.SCROLL_STATE_IDLE://up
					
					System.out.println("SCROLL_STATE_IDLE");
					imageLoader.loadDrawable(false);
					ListAdapter.this.notifyDataSetChanged();//刷新最后一屏数据.
					break;
					
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					
					System.out.println("SCROLL_STATE_TOUCH_SCROLL");
					imageLoader.loadDrawable(false);
					imageLoader.notifyAllWorkThread();
					break;
					
				default:
					
					imageLoader.loadDrawable(false);
					break;
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			
				
			}
		});
	}
	
	@Override
	public int getCount() {
		
		return lists.length;
	}

	@Override
	public Object getItem(int position) {
		
		return lists[position];
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if (convertView != null) {

			holder = (ContentHolder) convertView.getTag();
		} else {

			holder = new ContentHolder();
			convertView = inflater.inflate(R.layout.item, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.thumbnail);
			holder.name = (TextView) convertView.findViewById(R.id.item_name);
			holder.size = (TextView) convertView.findViewById(R.id.item_size);
			holder.icon.setImageResource(R.drawable.ic_launcher);
			convertView.setTag(holder);
		}
		return setContent(convertView, position, holder);
	}
	
	public View setContent(View convertView, int position, ContentHolder holder) {
	
		View view= null;
		view= setImage(holder, position, convertView);
		if (!urls.contains(position)) {

			urls.add(position);
			//如果这个时候缓存到iamges中是空的
//			images.add(new SoftReference<View>(convertView));
			paths.add(lists[position].getAbsolutePath());
		}
//		imageLoader.loadDrawable(false);
		view.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					
					imageLoader.loadDrawable(false);
					imageLoader.notifyAllWorkThread();
					break;

//				case MotionEvent.ACTION_MOVE:
//					
//					imageLoader.loadDrawable(true);
//					break;
//					
//				case MotionEvent.ACTION_CANCEL:
//					
//					imageLoader.loadDrawable(true);
//					break;
				default:
					
					imageLoader.loadDrawable(false);
					imageLoader.notifyAllWorkThread();
					break;
				}
				return false;
			}
		});
		return view;
	}
	
	@SuppressLint("HandlerLeak")
	private View setImage(final ContentHolder holder, final int position, View convertView) {
		
		imageLoader.loadDrawable(lists[position].getAbsolutePath(), 120, 120, new ImageCallBack() {
			
			@Override
			public void imageLoaded(Bitmap drawable) {

				//do something
			}
		}, new Handler(){
			
			@Override
			public void handleMessage(Message msg) {
				
				super.handleMessage(msg);
				String path= msg.getData().getString(Keys.PATH);
				Bitmap _bitmap= (Bitmap) msg.obj;
				int _position= 0;
				if(!paths.isEmpty()&& paths.indexOf(path)>= 0) {
					
					_position= urls.get(paths.indexOf(path));
				}
				holder.icon.setImageBitmap(_bitmap);
				holder.name.setText(lists[_position].getName()+ "--->>"+ _position);
				holder.size.setText((lists[_position].length()/ 1000/ 1000.0)+ "M");
			}
		});
		return convertView;
	}

	class ContentHolder {
		
		TextView name;
		TextView size;
		ImageView icon;
	}
}